package recursive;

import org.junit.Test;

public class MazePathFinding {
   @Test
    public void test() {
       int[][] maze = createMaze();
       showMaze(maze);
       findPath(maze, 1, 1);
       System.out.println("---------迷宫路径----------");
       showMaze(maze);
   }
    public int[][] createMaze(){
        int width=8;
        int height=8;
        int[][] maze = new int[height][width];//
        //迷宫装墙之左右围墙
        for (int i=0;i<height;i++){
                maze[i][0]=1;
                maze[i][width-1]=1;
        }
        //迷宫围墙之上下有墙
        for (int i=0;i<width;i++){
            maze[0][i]=1;
            maze[height-1][i]=1;
        }
        //迷宫加强之任意方块
//        Random random = new Random();
//        for (int i = 0; i < 3; i++) {
//            int htemp = random.nextInt(height);
//            int wtemp=random.nextInt(width);
//            maze[htemp][wtemp]=1;
//        }
        maze[4][1]=1;
        maze[4][2]=1;
        return maze;
    }
    public void showMaze(int [][] maze){
        for(int[] h:maze){
            for(int l:h){
                System.out.print(l+" ");
            }
            System.out.println(" ");
        }
    }
    public boolean findPath(int[][] maze,int x,int y){
        /**
         * 对程序做出如下声明:
         * 默认7,7为重点,1为墙,2为可通路径,3表示思路
         */
       if (maze[6][6]==2)
           return true;
       if (maze[x][y]!=0){
           return false;
       }else{
           maze[x][y]=2;
           System.out.println("----------当前迷宫------");
           showMaze(maze);
           if (findPath(maze,x,y-1)){
               //向上
               return true;
           }else if (findPath(maze,x-1,y)){
               //向左
               return true;
           }else if (findPath(maze,x,y+1)){
               //向下
               return true;
           }else if (findPath(maze,x+1,y)){
               //向右
               return true;
           }else{
               //当前节点向其他方向探索无果,
               //将当前节点标记为死路,然后回溯
               maze[x][y]=3;
               return false;
           }
       }
       //将当前节点标记,向四周探索寻找出路,默认顺序采用,上左下右

    }
    public void findPathByOrder(String order){

    }

}
